<template>
  <div class="index">
    <div class="header">
      <div class="left">
        <el-input v-model="input" placeholder="请输入姓名关键字" />
        <el-button size="medium" @click="handleSearch()">搜索</el-button>
      </div>
      <div class="right">
        <el-button size="medium" type="primary" icon="el-icon-arrow-left" @click="handleBack">返回</el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      :data="tableData"
      stripe
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="120"
      />
      <el-table-column
        prop="num"
        label="序号"
        width="150"
        type="index"
      />
      <el-table-column
        prop="studentName"
        label="姓名"
        width="180"
      />
      <el-table-column
        prop="phoneNumber"
        label="手机号"
        width="240"
      />
      <el-table-column
        prop="studentId"
        label="学号"
        width="240"
      />
      <el-table-column
        prop="gradeName"
        label="年级"
        width="180"
      />
      <el-table-column
        prop="className"
        label="班级"
        width="180"
      />
      <el-table-column
        label="操作"
        width="180"
      >
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleClick(scope.row)"><i class="el-icon-tickets" /></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pages">
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="handleCurrentChange" />
    </div>
  </div>
</template>

<script>
import { getStudentList } from '@/api/manage'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  components: { Pagination },
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      role: '',
      value: '0',
      input: '',
      currentPage: 1,
      total: 10,
      listQuery: {
        page: 1,
        limit: 10,
        title: undefined,
        province: undefined,
        city: undefined,
        area: undefined
      }
    }
  },

  created() {
    const data = {
      classId: this.$route.params.classId,
      pages: this.listQuery.page
    }
    getStudentList(data).then(res => {
      this.tableData = res.data.rows
      this.total = res.data.total
    })
  },

  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBack() {
      this.$router.go(-1)
    },
    handleSearch() {
      this.listQuery.page = 1
      const data = {
        classId: this.$route.params.classId,
        pages: this.listQuery.page,
        studentName: this.input
      }
      getStudentList(data).then(res => {
        this.tableData = res.data.rows
      })
    },
    handleClick(row) {
      console.log(row)
      this.$router.push(`/trainning/index/manage/list/detail/${row.studentId}`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${this.listQuery.page}`)
      const data = {
        classId: this.$route.params.classId,
        pages: this.listQuery.page,
      }
      getStudentList(data).then(res => {
        this.tableData = res.data.rows
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      const data = {
        classId: this.$route.params.classId,
        pages: this.currentPage
      }
      getStudentList(data).then(res => {
        this.tableData = res.data.rows
      })
    }
  }
}
</script>
<style scoped>
.header{
    display: flex;
    justify-content: space-between;
  }
  .left{
    display: flex;
    justify-content: flex-start;
  }
  .pages{
  text-align: center;
}
</style>
